<!DOCTYPE html>
<html lang="zh-CN">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录页</title>
    <style>
        /* CSS 样式 */
        body {
            font-family: Arial, sans-serif;
            background-color: #f4f4f4;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
            margin: 0;
        }

        .login-container {
            background: #fff;
            padding: 20px;
            border-radius: 8px;
            box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
            width: 300px;
        }

        h2 {
            text-align: center;
            margin-bottom: 20px;
        }

        .form-group {
            margin-bottom: 15px;
        }

        label {
            display: block;
            margin-bottom: 5px;
        }

        input {
            width: 100%;
            padding: 8px;
            box-sizing: border-box;
            border: 1px solid #ccc;
            border-radius: 4px;
        }
        input:focus{
            border-color: #007bff;
            outline: none;
        }

        button {
            width: 100%;
            padding: 10px;
            background-color: #28a745;
            color: #fff;
            border: none;
            border-radius: 4px;
            cursor: pointer;
        }

        button:hover {
            background-color: #218838;
        }

        .error-message {
            color: red;
            text-align: center;
            margin-top: 10px;
        }

        .register-button {
            margin-top: 10px;
            background-color: #007bff;
        }

        .register-button:hover {
            background-color: #0056b3;
        }
    </style>
</head>
<body>
<!-- HTML 结构 -->
<div class="login-container">
    <h2>IM</h2>
    <form id="loginForm">
        <div class="form-group">
<!--            <label for="appKey">租户:</label>-->
            <input type="text" id="appKey" name="appKey" value="test" required>
        </div>
        <div class="form-group">
<!--            <label for="username">用户名:</label>-->
            <input type="text" id="mobile" name="mobile" placeholder="请输入手机号" required>
        </div>
        <div class="form-group">
            <input type="password" id="password" name="password" placeholder="请输入密码" required>
        </div>
        <button type="submit">登录</button>
    </form>
    <p id="errorMessage" class="error-message"></p>
    <button id="registerButton" class="register-button">注册</button>
</div>

<script>
    // 配置接口服务前缀（IP 和端口）
    const API_BASE_URL = 'http://127.0.0.1:8080/web';
    // JavaScript 逻辑
    document.getElementById('loginForm').addEventListener('submit', async function (e) {
        e.preventDefault(); // 阻止表单默认提交行为

        const appKey = document.getElementById("appKey").value;
        const mobile = document.getElementById('mobile').value;
        const password = document.getElementById('password').value;
        const errorMessage = document.getElementById('errorMessage');

        // 简单的表单验证
        if (!mobile || !password) {
            errorMessage.textContent = '手机号和密码不能为空';
            return;
        }

        try {
            // 拼接完整的接口 URL
            const url = `${API_BASE_URL}/user/login`;

            // 模拟登录请求
            const response = await fetch(url, {
                method: 'POST',
                headers: {
                    'Content-Type': 'application/json',
                },
                body: JSON.stringify({ appKey, mobile, password }),
            });

            const result = await response.json();
            console.log(result)
            if (result.code == 200) {
                const userId = result.data.userId;

                const token = result.data.token;
                const wsUrl = result.data.gateUrl;

               // 方式 1：通过 URL 参数传递 userId
               // window.location.href = `/chat.html?userId=${userId}`;

                localStorage.setItem("userId", userId);
                localStorage.setItem("token", token);
                localStorage.setItem("wsUrl", wsUrl);

                // 登录成功，跳转到聊天页
                window.location.href = './chat.html';
            } else {
                // 登录失败，显示错误信息
                errorMessage.textContent = data.message || '登录失败';
            }
        } catch (error) {
            console.log(error)
            errorMessage.textContent = '网络错误，请稍后重试';
        }
    });

    // 注册按钮点击事件
    document.getElementById('registerButton').addEventListener('click', function () {
        // 跳转到注册页面
        window.location.href = './register.html';
    });
</script>
</body>
</html>
